package com.example.schrittzahler;

import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements SensorEventListener{
	private SensorManager sensorManager;
	private boolean color = false;
	private TextView viewX;
	private TextView viewY;
	private TextView viewZ;
	private TextView viewGrv;
	private TextView viewXmax;
	private TextView viewYmax;
	private TextView viewZmax;
	private TextView viewGrvmax;
	private long lastUpdate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.viewX = (TextView) findViewById(R.id.xValue);
		this.viewY = (TextView) findViewById(R.id.yValue);
		this.viewZ = (TextView) findViewById(R.id.zValue);
		this.viewGrv = (TextView) findViewById(R.id.grvValue);
		
		this.viewXmax = (TextView) findViewById(R.id.xValueBez);
		this.viewYmax = (TextView) findViewById(R.id.yValueBez);
		this.viewZmax = (TextView) findViewById(R.id.zValueBez);
		this.viewGrvmax = (TextView) findViewById(R.id.grvValueBez);
		
		this.viewX.setBackgroundColor(Color.GREEN);
		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		lastUpdate = System.currentTimeMillis();
	}
	  @Override
	  protected void onResume() {
	    super.onResume();
	    // register this class as a listener for the orientation and
	    // accelerometer sensors
	    sensorManager.registerListener(this,
	        sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
	        SensorManager.SENSOR_DELAY_NORMAL);
	  }

	  @Override
	  protected void onPause() {
	    // unregister listener
	    super.onPause();
	    sensorManager.unregisterListener(this);
	  }
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
		      getAccelerometer(event);
		    }
		
	}

	  private void getAccelerometer(SensorEvent event) {
		    float[] values = event.values;
		    // Movement
		    float x = values[0];
		    float y = values[1];
		    float z = values[2];
		    viewX.setText(String.valueOf(x));
		    viewY.setText(String.valueOf(y));
		    viewZ.setText(String.valueOf(z));
		    float accelationSquareRoot = (x * x + y * y + z * z)
		        / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
		    long actualTime = System.currentTimeMillis();
		    viewGrv.setText(String.valueOf(accelationSquareRoot));
		    if (accelationSquareRoot >= 2) //
		    {
		      if (actualTime - lastUpdate < 200) {
		        return;
		      }
		      lastUpdate = actualTime;
		      Toast.makeText(this, "Device was shuffed", Toast.LENGTH_SHORT)
		          .show();
		      viewXmax.setText(String.valueOf(x));
			  viewYmax.setText(String.valueOf(y));
			  viewZmax.setText(String.valueOf(z));
			  viewGrvmax.setText(String.valueOf(accelationSquareRoot));
		      if (color) {
		    	  viewX.setBackgroundColor(Color.GREEN);

		      } else {
		    	  viewX.setBackgroundColor(Color.RED);
		      }
		      color = !color;
		    }
		  }

}
